package com.gzy.xuexiziliao.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gzy.xuexiziliao.common.context.BaseContext;
import com.gzy.xuexiziliao.common.result.PageResult;
import com.gzy.xuexiziliao.mapper.AnonymousPostMapper;
import com.gzy.xuexiziliao.pojo.dto.AnonymousDTO;
import com.gzy.xuexiziliao.pojo.entity.AnonymousPost;
import com.gzy.xuexiziliao.pojo.entity.Doctor;
import com.gzy.xuexiziliao.pojo.entity.Student;
import com.gzy.xuexiziliao.service.AnonymousPostService;
import com.gzy.xuexiziliao.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class AnonymousPostServiceImpl implements AnonymousPostService {

    @Autowired
    private StudentService studentService;

    @Autowired
    private AnonymousPostMapper anonymousPostMapper;
    public void saveAnonymousPost(AnonymousDTO anonymousDTO) {
        AnonymousPost anonymousPost = new AnonymousPost();
        //1.复制
        BeanUtils.copyProperties(anonymousDTO, anonymousPost);

        //2.获取学生id
        Student student = studentService.getByOpenId(BaseContext.getCurrentId());
        anonymousPost.setStudentId(student.getStudentId());

        //3.配置其他信息
        anonymousPost.setLikeCount(0);
        anonymousPost.setCommentCount(0);
        anonymousPost.setCreateTime(LocalDateTime.now());
        anonymousPost.setUpdateTime(LocalDateTime.now());
        //
        anonymousPostMapper.saveAnonymousPost(anonymousPost);
    }

    /**
     * 帖子列表
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageResult page(Integer page, Integer pageSize, Byte category, LocalDate begin, LocalDate end) {
        log.info("查询帖子列表");
        //1.设置分页参数
        PageHelper.startPage(page,pageSize);
        //2.执行查询
        List<AnonymousPost> anonymousPostList=anonymousPostMapper.list(category, begin, end);
        Page<AnonymousPost> anonymousPage=(Page<AnonymousPost>) anonymousPostList;

        //3.封装对象
        PageResult pageResult=new PageResult(anonymousPage.getTotal(),anonymousPage.getResult());
        return pageResult;
    }
}
